package com.heima.article.service.impl;



import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
   //自定义方法,多表查询,自己写
    @Autowired ApArticleMapper articleMapper;
    //单页最大加载的数字

    private final static short MAX_PAGE_SIZE=50;
    //根据参数加载文章列表 1,2最新
    @Override
    public ResponseResult load(Short loadtype, ArticleHomeDto dto) {
        //校验参数
        //分页条数校验
        Integer size=dto.getSize();
        if (size==null||size==0){
            //size无值给个默认值为10
            size=10;
        }
        Math.min(size,MAX_PAGE_SIZE);//10-50
        dto.setSize(size);
        //类型参数校验
        //不等于1,2
        if (!loadtype.equals(ArticleConstants.LOADTYPE_LOAD_MORE)&&!loadtype.equals(ArticleConstants.LOADTYPE_LOAD_NEW)){
            //默认1
            loadtype=ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        //文章频道校验
        if (StringUtils.isEmpty(dto.getTag())){
            //默认
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        //时间校验
        if(dto.getMaxBehotTime() == null) dto.setMaxBehotTime(new Date());
        if(dto.getMinBehotTime() == null) dto.setMinBehotTime(new Date());

       //查数据
        List<ApArticle> apArticles = articleMapper.loadArticleList(dto, loadtype);
        //封装数据
        ResponseResult responseResult = ResponseResult.okResult(apArticles);
        return responseResult;
    }

    /**
     * 相关app文章
     * @param dto
     * @return
     */
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
       /* try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        //检查参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //文章表创建对象
        ApArticle apArticle=new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        //判断存在id
        if (dto.getId()==null){
            //不存在id  保存  文章  文章配置  文章内容,保存
            save(apArticle);
            //保存配置
            ApArticleConfig apArticleConfig=new ApArticleConfig(apArticle.getId());
            //添加配置
            apArticleConfigMapper.insert(apArticleConfig);
            //保存文章内容
            ApArticleContent apArticleContent=new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            //添加配置mapper中
            apArticleContentMapper.insert(apArticleContent);
        }else{
            //存在id 修改 文章 文章内容
            updateById(apArticle);
            //修改文章内容 1.先查文章比较id
            ApArticleContent apArticleContent=apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getId()));
            //设置文章内容
            apArticleContent.setContent(dto.getContent());
            //根据id修改配置
            apArticleContentMapper.updateById(apArticleContent);
        }
        //异步调用 生成静态文件上传到minio中
        articleFreemarkerService.buildArticleToMinIO(apArticle,dto.getContent());
        //结果返回 文章的id
        return ResponseResult.okResult(apArticle.getId());
    }
}
